var config = require('../../../utils/config');
var util = require('../../../utils/util');
var API_URL = config.API_URL;
var page;
var get_news = function (typeId, cb) {
    my.httpRequest({
        url: API_URL.news_list,
        data: {
            type: typeId,
            page: page
        },
        method: 'POST',
        success: function (res) {
            console.log(res)
            page++;
            if (res.data.data.length == 0) {
                my.alert({
                    title: '没有更多了',
                });
            }
            typeof cb == "function" && cb(res.data.data)
        },
        fail: function () {
            my.alert({
                title: '网络错误',
            });
        },
        complete: function () {
            my.hideLoading();
            my.hideToast();
            my.stopPullDownRefresh()
        }
    })
};
Page({
    data: {
        typeData: [
            {
                typeId: 0,
                typeName: '最新'
            }
        ],
        changeStyle: true

    },

    onLoad: function () {
        var that = this;
        page = 1;
        my.showLoading({
            content: '加载中...',
            delay: 500
        });

        my.httpRequest({
            url: API_URL.new_category,
            data: {},
            method: 'POST',
            success: function (res) {
                console.log(res)
                var typeData = that.data.typeData.concat(res.data.data);
                console.log(typeData)
                typeData = typeData.map(function (obj) { obj.class = "no_select"; return obj; })
                typeData[0].class = "select";
                that.setData({
                    typeData: typeData
                })

                console.log(res.data.data)
                // setTimeout(() => {
                //     my.hideLoading();
                // }, 2000)
            },
            fail: function () {
                // fail
            },
            complete: function () {
                // complete
            }
        })
        get_news(0, function (news) {
            that.setData({
                news: news,
                typeId: 0
            })
        })
    },
    // 新闻列表切换
    change: function (e) {
        console.log(e);
        var that = this;
        var changed = {};
        var typeId = e.currentTarget.dataset.id;
        console.log(typeId)
        // my.showToast({
        //     content: '加载中',
        //     type: 'success',
        //     duration: 1000
        // })
        my.showLoading({
            content: "加载中"
        });
        for (var i = 0; i < that.data.typeData.length; i++) {
            if (that.data.typeData[i].typeName == e.currentTarget.dataset.type) {
                changed['typeData[' + i + '].class'] = 'select';
                if (that.data.typeData[i].class == 'no_select') {
                    // 加载不同新闻
                    page = 1;
                    get_news(typeId, function (news) {
                        that.setData({
                            news: news,
                            typeId: typeId
                        })
                    })
                }
            }
            else {
                changed['typeData[' + i + '].class'] = 'no_select';
            }
        }
        console.log(changed)
        this.setData(changed);
    },
    onPullDownRefresh: function () {
        var that = this;
        var typeId = this.data.typeId;
        that.setData({
            changeStyle:false
        })
        page = 1;
        my.showLoading({
            content: "刷新中...",
            delay: 500,
            success: (res) => {
                setTimeout(function () {
                    my.hideLoading();
                }, 1500);
            }
        });
        get_news(typeId, function (news) {
            that.setData({
                news: news,
                changeStyle:true
            })
        })
    },
    // 下拉加载
    onReachBottom: function () {
        var that = this;
        var typeId = this.data.typeId;
        my.showLoading({
            content: '加载中',
            delay: 0,
            success: () => {
                get_news(typeId, function (news) {
                    that.setData({
                        news: that.data.news.concat(news),

                    })
                })
            },
            complete: () => {
                my.hideLoading();
            },
            fail: () => {
                my.hideLoading();

            }
        })

    },
    // 跳转新闻详情页面
    news: function (e) {
        var id = e.currentTarget.dataset.id;
        var typeId = e.currentTarget.dataset.type;
        my.navigateTo({
            url: '/pages/article/news/news?id=' + id + '&typeId=' + typeId
        })
    }
})